package com.zliblike_platform.service.impl;

import com.zliblike_platform.entity.User;
import com.zliblike_platform.mapper.RankingMapper;
import com.zliblike_platform.mapper.UserMapper;
import com.zliblike_platform.service.RankingService;
import com.zliblike_platform.vo.RankingVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

/**
 * 排行榜服务实现类
 */
@Service
@Slf4j
public class RankingServiceImpl implements RankingService {

    @Autowired
    private RankingMapper rankingMapper;
    
    @Autowired
    private UserMapper userMapper;
    
    private static final Map<String, Integer> SCORE_RULES = new HashMap<>();
    
    static {
        // 初始化积分规则
        SCORE_RULES.put("upload", 5);    // 上传书籍：+5分/本
        SCORE_RULES.put("edit", 3);      // 修改书籍信息：+3分/次
        SCORE_RULES.put("download", 2);  // 下载：+2分/次
        SCORE_RULES.put("read", 2);      // 阅读：+2分/次
        SCORE_RULES.put("comment", 2);   // 评论：+2分/次
    }

    @Override
    public List<RankingVO> getTotalRanking(Integer limit) {
        // 获取总积分排行榜，按总积分降序排序
        return rankingMapper.selectTotalRanking(limit);
    }

    @Override
    public List<RankingVO> getUploadRanking(Integer limit) {
        // 获取上传积分排行榜，按上传积分降序排序
        return rankingMapper.selectUploadRanking(limit);
    }

    @Override
    public List<RankingVO> getEditRanking(Integer limit) {
        // 获取编辑积分排行榜，按编辑积分降序排序
        return rankingMapper.selectEditRanking(limit);
    }

    @Override
    @Transactional
    public void updateUploadScore(Long userId, Integer points) {
        // 更新用户上传积分
        rankingMapper.updateUploadScore(userId, points);
        // 更新总积分
        rankingMapper.updateTotalScore(userId);
    }

    @Override
    @Transactional
    public void updateEditScore(Long userId, Integer points) {
        // 更新用户编辑积分
        rankingMapper.updateEditScore(userId, points);
        // 更新总积分
        rankingMapper.updateTotalScore(userId);
    }

    @Override
    @Transactional
    public void updateDownloadScore(Long userId, Integer points) {
        // 更新用户下载积分
        rankingMapper.updateDownloadScore(userId, points);
        // 更新总积分
        rankingMapper.updateTotalScore(userId);
    }

    @Override
    @Transactional
    public void updateReadScore(Long userId, Integer points) {
        // 更新用户阅读积分
        rankingMapper.updateReadScore(userId, points);
        // 更新总积分
        rankingMapper.updateTotalScore(userId);
    }

    @Override
    @Transactional
    public void updateCommentScore(Long userId, Integer points) {
        // 更新用户评论积分
        rankingMapper.updateCommentScore(userId, points);
        // 更新总积分
        rankingMapper.updateTotalScore(userId);
    }

    @Override
    public Integer selectUserTotalScore(Long userId) {
        // 获取用户总积分
        return rankingMapper.selectUserTotalScore(userId);
    }

    @Override
    @Scheduled(cron = "0 0 0 1 * ?") // 每月1号凌晨执行
    @Transactional
    public void updateRankingRewards() {
        // 获取总积分前三名的用户ID
        List<Long> topUserIds = rankingMapper.selectTopUserIds(3);
        
        // 为这些用户升级账户等级为高级账户
        for (Long userId : topUserIds) {
            userMapper.updateUserLevel(userId, 2); // 假设2代表高级账户等级
        }
    }
} 